package com.zzedu.web.controller.system;

import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.domain.entity.SysMenu;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 *
 * @author smarthome
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private ISysMenuService menuService;

    @PreAuthorize("@permission.hasAnyPermissions('system:menu:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysMenu menu) {
        List<SysMenu> menuList = menuService.selectMenuList(menu, null);
        return AjaxResult.success(menuList);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:menu:query')")
    @RequestMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return success(menuService.selectMenuById(menuId));
    }

    /**
     * 删除菜单
     */
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@permission.hasAnyPermissions('system:menu:remove')")
    @RequestMapping("/remove/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        return toAjax(menuService.deleteMenuById(menuId));
    }


    /**
     * 新增保存菜单
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:menu:add')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysMenu menu) {
        return toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改保存菜单
     */
    @PreAuthorize("@permission.hasAnyPermissions('system:menu:edit')")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SysMenu menu) {
        return toAjax(menuService.updateMenu(menu));
    }


    /**
     * 加载对应角色菜单列表树
     */
    @RequestMapping(value = "/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        List<SysMenu> menus = menuService.selectMenuList(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }
    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysMenu menu)
    {
        List<SysMenu> menus = menuService.selectMenuList(menu, getUserId());
        return success(menuService.buildMenuTreeSelect(menus));
    }
    /**
     * 校验菜单名称
     */
    @PostMapping("/checkMenuNameUnique")
    @ResponseBody
    public boolean checkMenuNameUnique(SysMenu menu) {
        return menuService.checkMenuNameUnique(menu);
    }

}